
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import com.google.gdata.client.finance.FinanceService;
import com.google.gdata.data.extensions.Money;
import com.google.gdata.data.finance.PortfolioData;
import com.google.gdata.data.finance.PortfolioEntry;
import com.google.gdata.data.finance.PortfolioFeed;
import com.google.gdata.data.finance.PositionData;
import com.google.gdata.data.finance.PositionEntry;
import com.google.gdata.data.finance.PositionFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class StockData
{
	FinanceService myService; 
	
	StockData()
	{
	String username= "satyajeet.financial.technology@gmail.com";
	String password = "fintech1";
	myService = new FinanceService("exampleCo-exampleApp-1");
	try {
		myService.setUserCredentials(username,password);
	} catch (AuthenticationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
	
	public ArrayList<Fstock> getAll()
	{
		ArrayList<Fstock> stocklist = new ArrayList<Fstock>();
		String feedUrl = "http://finance.google.com/finance/feeds/default/portfolios/";
		try {
			PortfolioFeed portfoliofeed = myService.getFeed(new URL(feedUrl),PortfolioFeed.class);
			for(int loop=0;loop<portfoliofeed.getEntries().size();loop++)
			{
				PortfolioEntry portfolioentry = portfoliofeed.getEntries().get(loop);
				PortfolioData portfoliodata = portfolioentry.getPortfolioData();
				String feedURL2 = feedUrl + (loop+1) + "/positions?returns=true";
				PositionFeed positionfeed = myService.getFeed(new URL(feedURL2), PositionFeed.class);
				for(int loop2=0;loop2<positionfeed.getEntries().size();loop2++)
				{
					Fstock ftemp = new Fstock();
					PositionEntry positionentry = positionfeed.getEntries().get(loop2);
					ftemp.setSymbol(positionentry.getSymbol().getSymbol());
					ftemp.setFullname(positionentry.getSymbol().getFullName());
					ftemp.setExchange(positionentry.getSymbol().getExchange());
					PositionData positiondata = positionentry.getPositionData();
					ftemp.setStockcount(positiondata.getShares());
					if (positiondata.getMarketValue() != null)
					{
						for (int i = 0; i < positiondata.getMarketValue().getMoney().size(); i++) {
					        Money m = positiondata.getMarketValue().getMoney().get(i);
					        //System.out.printf("\t\tThis position is worth %.2f %s.\n", m.getAmount(), m.getCurrencyCode());
					        double worth =  m.getAmount();
					        float lastprice = (float)(worth/positiondata.getShares());
					        ftemp.setLastprice(lastprice);
						}
					}
					if(!(positiondata.getCostBasis() == null))
			  		{
			   	  		for (int i = 0; i < positiondata.getCostBasis().getMoney().size(); i++)
			   	  		{
				   	         Money m = positiondata.getCostBasis().getMoney().get(i);
				   	         System.out.printf("\t\tThis position cost %.2f %s.\n", m.getAmount(), m.getCurrencyCode());
				   	         float originalprice = (float)(m.getAmount()/positiondata.getShares());
				   	  		
				   	         ftemp.setOriginalprice(originalprice);
			   	         
			   	  		}
			  			
			  		}
					if(!(positiondata.getDaysGain() == null))
			   	  	{
			   	  		for (int i = 0; i < positiondata.getDaysGain().getMoney().size(); i++) 
			   	  		{
				  				Money money = positiondata.getDaysGain().getMoney().get(i);
				  				float daysgain = (float)(money.getAmount()/positiondata.getShares());
				  				ftemp.setDaysgain(daysgain);
			   	  		}
			   	  	}// days gain ends
					if(!(positiondata.getGain() == null))
			   	  	{
			       	  	for (int i = 0; i < positiondata.getGain().getMoney().size(); i++) 
				  			{
				  				Money money = positiondata.getGain().getMoney().get(i);
				  				//System.out.printf("\t\tThis position has a total gain of %.2f %s.\n", money.getAmount(), money.getCurrencyCode());
				  				Double totalgain = money.getAmount()/positiondata.getShares();
				  				ftemp.setTotalgain(totalgain.floatValue());
				  			}
			   	  		
			   	  	}
					
					// Let us add ftemp to the arraylist
					stocklist.add(ftemp);
				}
			
			
			}// for ends pf feed
			
			
			
			
		}
		catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		return stocklist;
	}
	
	
}

 class Fstock
{
	 String symbol,fullname, exchange;
	 Float percentgain, lastprice, originalprice, totalgain, daysgain;
	Double stockcount;
	 
	 public String getSymbol() {
		return symbol;
	}
	public void setSymbol(String symbol) {
		this.symbol = symbol;
	}
	public String getFullname() {
		return fullname;
	}
	public void setFullname(String fullname) {
		this.fullname = fullname;
	}
	public String getExchange() {
		return exchange;
	}
	public void setExchange(String exchange) {
		this.exchange = exchange;
	}
	public Double getStockcount() {
		return stockcount;
	}
	public void setStockcount(Double stockcount) {
		this.stockcount = stockcount;
	}
	public Float getPercentgain() {
		return percentgain;
	}
	public void setPercentgain(Float percentgain) {
		this.percentgain = percentgain;
	}
	public Float getLastprice() {
		return lastprice;
	}
	public void setLastprice(Float lastprice) {
		this.lastprice = lastprice;
	}
	public Float getOriginalprice() {
		return originalprice;
	}
	public void setOriginalprice(Float originalprice) {
		this.originalprice = originalprice;
	}
	public Float getTotalgain() {
		return totalgain;
	}
	public void setTotalgain(Float totalgain) {
		this.totalgain = totalgain;
	}
	public Float getDaysgain() {
		return daysgain;
	}
	public void setDaysgain(Float daysgain) {
		this.daysgain = daysgain;
	}
	 
	 
	
}